package com.hyg.oa.officing.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hyg.oa.common.PageUtils;
import com.hyg.oa.common.Pagination;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyg.oa.common.UUIDUtils;
import com.hyg.oa.officing.model.Officing;
import com.hyg.oa.officing.service.OfficingService;


/**
 * Controller类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/officing") 
public class OfficingController {
	
	@Autowired
	private OfficingService officingService;
	
	/**
	 * page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		//总数
		long totalCount = this.officingService.findTotalCount();
		//page对象
		Pagination pagination = PageUtils.create(request, totalCount);
		
		//数据
		List<Officing> list = this.officingService.findPage(pagination.getPageNo(), pagination.getPageSize());
		model.put("list", list);
		model.put("pagination", pagination);
		
		return new ModelAndView("/jsp/officing/list.jsp", model);
	}
	
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String,Object>();
		
		List<Officing> list = this.officingService.findAll();
		model.put("list", list);
		
		return new ModelAndView("/jsp/officing/list.jsp", model);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/jsp/officing/add.jsp");
	}
	
	
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String username = request.getParameter("username");
			String type = request.getParameter("type");
			String name = request.getParameter("name");
			String unit = request.getParameter("unit");
			String count = request.getParameter("count");
			String spec = request.getParameter("spec");
			String reason = request.getParameter("reason");
			
			if(StringUtils.isEmpty(count)) {
				count = "0";
			}
		
			Officing officing = new Officing();
			officing.setUsername(username);
			officing.setType(type);
			officing.setName(name);
			officing.setUnit(unit);
			officing.setCount(Integer.parseInt(count));
			officing.setSpec(spec);
			officing.setReason(reason);
			officing.setCreatetime(new Date());
			officing.setId(UUIDUtils.generate());
			this.officingService.save(officing);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 用户编辑
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		Officing officing = this.officingService.findById(id);
		model.put("officing", officing);
		
		return new ModelAndView("/jsp/officing/edit.jsp", model);
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			String username = request.getParameter("username");
			String type = request.getParameter("type");
			String name = request.getParameter("name");
			String unit = request.getParameter("unit");
			String count = request.getParameter("count");
			String spec = request.getParameter("spec");
			String reason = request.getParameter("reason");
			
			if(StringUtils.isEmpty(count)) {
				count = "0";
			}
			
			Officing officing = this.officingService.findById(id);
			officing.setUsername(username);
			officing.setType(type);
			officing.setName(name);
			officing.setUnit(unit);
			officing.setCount(Integer.parseInt(count));
			officing.setSpec(spec);
			officing.setReason(reason);
			officing.setUpdatetime(new Date());
			this.officingService.update(officing);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			this.officingService.delete(id);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
}
